﻿using EntityClassMaker.DataBaseServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EntityClassMaker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private CallSqlServer callSqlServer = CallSqlServer.GetCallSqlServer();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //查询数据库中所有数据库名称
            DataBaseName.ItemsSource = callSqlServer.GetAllDbName("master", "select name from master..sysdatabases");
            DataBaseName.SelectedIndex = 0;
        }

        /// <summary>
        /// 总数据库下拉列表打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataBaseName_DropDownClosed(object sender, EventArgs e)
        {
            //查询数据库中的所有表
            Initial.ItemsSource = callSqlServer.GetAllDbName(DataBaseName.Text, "select * from sysobjects where xtype='U' order by name");
            Initial.SelectedIndex = 0;
        }

        /// <summary>
        /// 分数据库表名打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Initial_DropDownClosed(object sender, EventArgs e)
        {
            if ((bool)ISTwoBingin.IsChecked)
            {
                PreviewCode.Text = GetSQLFieldsConvertNetType(DataBaseName.Text, Initial.Text, true);
            }
            else
            {
                PreviewCode.Text = GetSQLFieldsConvertNetType(DataBaseName.Text, Initial.Text);
            }

        }

        /// <summary>
        /// 根据数据库中查询出来的值来生成cs类(不支持双向绑定)
        /// </summary>
        /// <param name="ChoiceODBName"></param>
        /// <param name="SelectDBName"></param>
        /// <returns></returns>
        private string GetSQLFieldsConvertNetType(string ChoiceODBName, string SelectDBName)
        {
            Dictionary<string, string> Previe = callSqlServer.GetAllDbNameType(ChoiceODBName, "select sc.name,st.name as Type from syscolumns sc,systypes st where " +
                                                                                             "sc.xtype=st.xtype and sc.id in(select id from sysobjects where xtype='U' and name='" + SelectDBName + "');");

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("\r\n");
            stringBuilder.AppendLine("public class " + SelectDBName);

            stringBuilder.AppendLine("{");

            foreach (var item in Previe)
            {
                switch (item.Value)
                {
                    case "int": stringBuilder.AppendLine("public int " + item.Key + " {get;set;}"); break;
                    case "datetime": stringBuilder.AppendLine("public DateTime " + item.Key + " {get;set;}"); break;
                    case "decimal": stringBuilder.AppendLine("public decimal " + item.Key + " {get;set;}"); break;
                    case "float": stringBuilder.AppendLine("public float " + item.Key + " {get;set;}"); break;
                    case "smallint": stringBuilder.AppendLine("public short " + item.Key + " {get;set;}"); break;

                    case "text":
                    case "nchar":
                    case "ntext":
                    case "nvarchar":
                    case "varchar ": stringBuilder.AppendLine("public string " + item.Key + " {get;set;}"); break;

                    default: stringBuilder.AppendLine("public string " + item.Key + " {get;set;}"); break;
                }
            }
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("}");

            return stringBuilder.ToString();
        }

        /// <summary>
        /// 双向绑定
        /// </summary>
        /// <param name="ChoiceODBName"></param>
        /// <param name="SelectDBName"></param>
        /// <param name="bl"></param>
        /// <returns></returns>
        private string GetSQLFieldsConvertNetType(string ChoiceODBName, string SelectDBName, bool bl)
        {
            Dictionary<string, string> Previe = callSqlServer.GetAllDbNameType(ChoiceODBName, "select sc.name,st.name as Type from syscolumns sc,systypes st where " +
                                                                                             "sc.xtype=st.xtype and sc.id in(select id from sysobjects where xtype='U' and name='" + SelectDBName + "');");

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("\r\n");
            stringBuilder.AppendLine("public class " + SelectDBName + ": INotifyPropertyChanged");

            stringBuilder.AppendLine("{");


            stringBuilder.AppendLine("public event PropertyChangedEventHandler PropertyChanged;");
            stringBuilder.AppendLine("public void NotifyPropertyChanged(string propertyName)");
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine("PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));");
            stringBuilder.AppendLine("}");


            foreach (var item in Previe)
            {
                switch (item.Value)
                {
                    case "int": stringBuilder.AppendLine(ReturnStr("int", item.Key)); break;
                    case "datetime": stringBuilder.AppendLine(ReturnStr("DateTime", item.Key)); break;
                    case "decimal": stringBuilder.AppendLine(ReturnStr("decimal", item.Key)); break;
                    case "float": stringBuilder.AppendLine(ReturnStr("decimal", item.Key)); break;
                    case "smallint": stringBuilder.AppendLine(ReturnStr("short", item.Key)); break;

                    case "text":
                    case "nchar":
                    case "ntext":
                    case "nvarchar":
                    case "varchar ": stringBuilder.AppendLine(ReturnStr("string", item.Key)); break;

                    default: stringBuilder.AppendLine(ReturnStr("string", item.Key)); break;
                }
            }
            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("}");

            return stringBuilder.ToString();
        }


        private string ReturnStr(string Type, string TypeName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine($"private {Type} " + TypeName.ToLower() + ";");
            stringBuilder.AppendLine($"public {Type} " + TypeName);
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine("get");
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine("return " + TypeName.ToLower() + ";");
            stringBuilder.AppendLine("}");

            stringBuilder.AppendLine("set");
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLine(TypeName.ToLower() + "=value;");
            stringBuilder.AppendLine(@"NotifyPropertyChanged(""" + TypeName + "\");");
            stringBuilder.AppendLine("}");
            stringBuilder.AppendLine("}");

            return stringBuilder.ToString();
        }

        /// <summary>
        /// 是否双向绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ISTwoBingin_Click(object sender, RoutedEventArgs e)
        {
            if ((bool)(e.Source as CheckBox).IsChecked)
            {
                PreviewCode.Text = GetSQLFieldsConvertNetType(DataBaseName.Text, Initial.Text, true);
            }
            else
            {
                PreviewCode.Text = GetSQLFieldsConvertNetType(DataBaseName.Text, Initial.Text);
            }
        }
    }
}
